package com.zhhs.galaxyott.adapters;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.movieDetailActivity.MovieDetailActivity;
import com.zhhs.galaxyott.beans.CategoryMoviesBean;
import com.zhhs.galaxyott.utils.Logs;

public class MoreMoviesAdapter extends BaseAdapter {

    private Context context;
    private List<CategoryMoviesBean> list;

    private TextView movie_name;

    public MoreMoviesAdapter(Context context, List<CategoryMoviesBean> list) {

        this.context = context;
        this.list = list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return list.get(arg0);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (null == convertView) {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_more_movies_item, null);
            holder.poster_layout = (RelativeLayout) convertView.findViewById(R.id.poster_layout);

            holder.posterImg = (ImageView) convertView.findViewById(R.id.poster);
            holder.poster_tag = (ImageView) convertView.findViewById(R.id.poster_tag);
            holder.movieName = (TextView) convertView.findViewById(R.id.poster_name);
            convertView.setTag(holder);

        } else {
            holder = (Holder) convertView.getTag();
        }
        // 如果是Glide则必须是下列方式设置Tag
        holder.poster_layout.setTag(R.id.poster_layout, list.get(position).getId());
        // holder.posterImg.setTag(R.id.poster, list.get(position).getId());
        holder.movieName.setText(list.get(position).getName());
        Glide.with(context).load(list.get(position).getImgurl()).placeholder(R.drawable.movie_item_default).crossFade()
                .into(holder.posterImg);

        holder.poster_layout.setOnClickListener(mOnClickListener);
        // holder.posterImg.setOnClickListener(mOnClickListener);

        Logs.logD("DEBUG_dt", "this is list.get(position).getId()" + list.get(position).getId());

        holder.poster_layout.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Logs.logD("DEBUG_lc", "THIS IS onFocusChange");
                    ((TextView) ((RelativeLayout) v).getChildAt(3)).setSelected(true);
                    ((View) ((RelativeLayout) v).getChildAt(1)).setBackgroundResource(R.drawable.border_select);
                    ;

                } else {
                    ((TextView) ((RelativeLayout) v).getChildAt(3)).setSelected(false);
                    ((View) ((RelativeLayout) v).getChildAt(1)).setBackgroundResource(R.drawable.border_normal);
                }

            }
        });
        // holder.posterImg.setOnFocusChangeListener(new OnFocusChangeListener()
        // {
        //
        // @Override
        // public void onFocusChange(View v, boolean hasFocus) {
        // if (hasFocus) {
        // Logs.logD("DEBUG_lc", "THIS IS onFocusChange");
        // ((TextView) ((RelativeLayout)
        // v.getParent()).getChildAt(2)).setSelected(true);
        //
        // } else {
        // ((TextView) ((RelativeLayout)
        // v.getParent()).getChildAt(2)).setSelected(false);
        // }
        //
        // }
        // });

        return convertView;
    }

    OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(context, MovieDetailActivity.class);
            Logs.logD("DEBUG_AD", "THIS IS -->POSITION:" + v.getTag());
            // 此处与上处Glide设置方式对应
            intent.putExtra("movieid", (Integer) v.getTag(R.id.poster_layout));
            context.startActivity(intent);
        }
    };

    public class Holder {
        RelativeLayout poster_layout;
        ImageView posterImg, poster_tag;
        TextView movieName;
    }
}
